import useSWR from 'swr';
import { statisticsOrderChartApi } from '../api/global2';
import { useAuthStore2 } from '@/app/cg06/store/useAuthStore2';

export const useGlobeData = () => {
	const token = useAuthStore2(s => s.token);
	const { data, error, isLoading } = useSWR(
		() => (token ? '/report/large-screen/gyl-statisticsChart' : null),
		statisticsOrderChartApi,
		{revalidateOnFocus: false}
	);

	/**年底采购金额*/
	const purchaseTendencyData: {
		xAxis: string[];
		seriesData: {
		  name: string;
		  data: number[];
		  areaTopColor: string;
		  labelColor: string;
		  lineStyle: string;
		}[];
	  } = {
		xAxis: [],
		seriesData: [
		  {
			name: '',
			data: [],
			areaTopColor: '#47B0FA',
			labelColor: '#D8D8D8',
			lineStyle: '#47B0FA',
		  }
		],
	  };
	
	purchaseTendencyData.xAxis = data?.poAmountList.map((i: any) => i.month.slice(2, 4) + '年' + i.month.slice(4) + '月' ) || [];
	purchaseTendencyData.seriesData[0].data = data?.poAmountList.map((i: any) => i.quantity1) || [];

	/**
	 * 库存变化趋势图
	 */

	const inventoryTendencyData = {
		xAxis: [],
		seriesData: [
			{
				name: '国内',
				data: [],
				areaTopColor: '#E28F25',
				labelColor: '#D8D8D8',
				lineStyle: '#E28F25'
			},
			{
				name: '国外',
				data: [],
				areaTopColor: '#47B0FA',
				labelColor: '#D8D8D8',
				lineStyle: '#47B0FA'
			}
		]
	};

	const inventoryInChartListLength = data?.inventoryInChartList.length || 0;

	inventoryTendencyData.xAxis = data?.inventoryInChartList.slice(inventoryInChartListLength - 12, inventoryInChartListLength).map((i: any) => i.week.slice(2, 4) + '年' + i.week.slice(4) + '周' ) || [];
	inventoryTendencyData.seriesData[0].data = data?.inventoryInChartList.slice(inventoryInChartListLength - 12, inventoryInChartListLength).map((i: any) => i.quantity1) || [];
	inventoryTendencyData.seriesData[1].data = data?.inventoryOutChartList.slice(inventoryInChartListLength - 12, inventoryInChartListLength).map((i: any) => i.quantity1) || [];

	/**
	 * 出库变化趋势图
	 */
	const shipmentTendencyData = {
		xAxis: [],
		seriesData: [
			{
				name: '国内',
				data: [],
				areaTopColor: '#E28F25',
				labelColor: '#D8D8D8',
				lineStyle: '#E28F25'
			},
			{
				name: '国外',
				data: [],
				areaTopColor: '#47B0FA',
				labelColor: '#D8D8D8',
				lineStyle: '#47B0FA'
			}
		]
	};

	const shipmentPcsInChartListLength = data?.shipmentPcsInChartList.length || 0;

	shipmentTendencyData.xAxis = data?.shipmentPcsInChartList.slice(shipmentPcsInChartListLength - 12, shipmentPcsInChartListLength).map((i: any) => i.week.slice(2, 4) + '年' + i.week.slice(4) + '周' ) || [];
	shipmentTendencyData.seriesData[0].data = data?.shipmentPcsInChartList.slice(shipmentPcsInChartListLength - 12, shipmentPcsInChartListLength).map((i: any) => i.quantity1) || [];
	shipmentTendencyData.seriesData[1].data = data?.shipmentPcsOutChartList.slice(shipmentPcsInChartListLength - 12, shipmentPcsInChartListLength).map((i: any) => i.quantity1) || [];

	/**
	 * 在库库存分布
	 */

	const onwayInventoryBarData: {
		yAxis: string[];
		seriesData: {
		  name: string;
		  data: number[];
		  labelColor: string;
		}[];
	  } = {
		yAxis: [],
		seriesData: [
		  {
			name: '',
			data: [],
			labelColor: '#88D0FF',
		  }
		],
	};
	
	onwayInventoryBarData.yAxis = data?.onwayInventoryList.map((i: any) => i.warehouseName) || [];
	onwayInventoryBarData.seriesData[0].data = data?.onwayInventoryList.map((i: any) => i.quantity1) || [];

	/**
	 * 仓库库存分布
	 */

	const warehouseInventoryBarData = {
		yAxis: [],
		seriesData: [
			{
				name: '',
				data: [],
				labelColor: '#88D0FF'
			}
		]
	};

	warehouseInventoryBarData.yAxis = data?.warehouseInventoryList.map((i: any) => i.warehouseName) || [];
	warehouseInventoryBarData.seriesData[0].data = data?.warehouseInventoryList.map((i: any) => i.quantity1) || [];

	/**
	 * 仓库出库分布
	 */

	const warehouseShipmentBarData = {
		yAxis: [],
		seriesData: [
			{
				name: '',
				data: [],
				labelColor: '#88D0FF'
			}
		]
	};

	warehouseShipmentBarData.yAxis = data?.warehouseShipmentList.map((i: any) => i.warehouseName) || [];
	warehouseShipmentBarData.seriesData[0].data = data?.warehouseShipmentList.map((i: any) => i.quantity1) || [];

	type TableItem = [string, string, string];

	/**
	 * 采购信息 table
	 */
	const poList: TableItem[] = data?.poList?.map(
		(item: any) => [
		  item.poNo || '',
		  item.customerName || '',
		  item.quantity?.toLocaleString() || '',
		],
	) || [['', '', '']];
	
	  /**
	   * 统计数据
	   */
	  const statisticsData: { [key: string]: number | string } = {
		'代发出库数': data?.gylTotalPoNums || 0,
		'FBA出库数': data?.gylTotalPoAmounts || 0,
		'入库数': data?.gylTotalWarehouseAreas || 0,
		'全球库存数': data?.gylTotalInventoryNums || 0,
	  };
	
	/**
	 * 没有视频的仓库展示内容
	 */

	const warehouseMapOfChartMap = data?.warehouseMapOfChartMap || {};
	
	return {
		purchaseTendencyData: purchaseTendencyData,
		inventoryTendencyData: inventoryTendencyData,
		shipmentTendencyData: shipmentTendencyData,
		onwayInventoryBarData: onwayInventoryBarData,
		warehouseInventoryBarData: warehouseInventoryBarData,
		warehouseShipmentBarData: warehouseShipmentBarData,
		poList: poList,
		statisticsData: statisticsData,
		warehouseMapOfChartMap,
		isLoading,
		isError: error
	};
};

